package aries.utils;

/******************************************************************************
 * You may modify, publish, and use Aries for your own purpose. BUT
 * DON'T REMOVE THIS TEXT.
 * Visit my facebook: http://www.facebook.com/freeskies or
 * Follow my twitter: @freeskys
 *
 *
 * Author: Harditya Rahmat Ramadhan
 * Licence: GPLv2
 *****************************************************************************/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class FileUtil extends Util {

    private File file;

    /**
     * Initialize FileUtil.
     * 
     * @param file 
     */
    public FileUtil(String file) {
        this.file = new File(file);
    }
    
    /**
     * Initialize FileUtil.
     * 
     * @param file 
     */
    public FileUtil(File file) {
        this.file = file;
    }
    
    /**
     * Get the file.
     * 
     * @return File
     */
    public File getFile() {
        return this.file;
    }
    
    /**
     * Copying a file.
     * 
     * @param destination
     * @throws FileNotFoundException
     * @throws IOException 
     */
    public void copyTo(String destination) throws FileNotFoundException, IOException {
        copyTo(new File(destination));
    }
    
    /**
     * Copying a file.
     * 
     * @param destination
     * @throws FileNotFoundException
     * @throws IOException 
     */ 
    public void copyTo(File destination) throws FileNotFoundException, IOException {
        FileChannel in = new FileInputStream(file).getChannel();
        FileOutputStream out = new FileOutputStream(destination);
        int maxCount = (64*1024*1024) - (32*1024);
        long size = in.size();
        long position = 0;
        while (position < size) {
            position += in.transferTo(position, maxCount, out.getChannel());
        }
        if (in != null)
            in.close();
        if (out != null)
            out.close();
    }
        
}